package ndk.epro.com.umengloacin

import android.Manifest
import android.content.Intent
import android.os.Build
import android.support.v4.app.ActivityCompat
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import com.bumptech.glide.Glide
import com.umeng.socialize.UMAuthListener
import com.umeng.socialize.UMShareAPI
import com.umeng.socialize.bean.SHARE_MEDIA
import kotlinx.android.synthetic.main.activity_main.*
import org.jetbrains.anko.toast

class MainActivity : AppCompatActivity(), View.OnClickListener {
    internal var mPermissionList = arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE, Manifest.permission.READ_LOGS, Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.SET_DEBUG_APP, Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.GET_ACCOUNTS, Manifest.permission.WRITE_APN_SETTINGS)
    private val umAuthListener: UMAuthListener? = null
    private val TAG = "MainActivity"
    private var headImg: ImageView? = null

    private val login: TextView
        get() {
            val textView = findViewById(R.id.login) as TextView
            return textView
        }
    private val fbLogin: TextView
        get() {
            val textView = findViewById(R.id.login) as TextView
            return textView
        }

    private val resultTv:TextView
        get() = findViewById(R.id.result) as TextView


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
//        headImg = findViewById(R.id.head_picture) as ImageView
        login.setOnClickListener(this)
        fblogin.setOnClickListener(this)
        checkPermission()
    }

    private fun checkPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            ActivityCompat.requestPermissions(this, mPermissionList, 123)
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {}


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data ?: null)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.login -> login(SHARE_MEDIA.QQ)
            R.id.fblogin -> login(SHARE_MEDIA.FACEBOOK)
        }
    }

    private fun login(name: SHARE_MEDIA) {
        val authListener = object : UMAuthListener {
            /**
             * @desc 授权开始的回调
             * *
             * @param platform 平台名称
             */
            override fun onStart(platform: SHARE_MEDIA) {

            }

            /**
             * @desc 授权成功的回调
             * *
             * @param platform 平台名称
             * *
             * @param action 行为序号，开发者用不上
             * *
             * @param data 用户资料返回
             */
            override fun onComplete(platform: SHARE_MEDIA, action: Int, data: Map<String, String>) {

                Toast.makeText(this@MainActivity, "成功了", Toast.LENGTH_LONG).show()
                Log.e(TAG, "action = " + action + "\nplayform = " + JsonFormat.format(platform.toString()) +
                        "\ndata = " + JsonFormat.format(data.toString()))
                val snsPlatform = platform.toSnsPlatform()
                Log.e(TAG, "snsplayform = " + snsPlatform.mGrayIcon + "\n" + snsPlatform.mIcon + "\n" +
                        snsPlatform.mIndex + "\n" + snsPlatform.mKeyword + "\n" + snsPlatform.mPlatform + "\n" + snsPlatform.mShowWord)

//                Glide.with(this@MainActivity).load(data["iconurl"]).into(headImg)
                resultTv.setText("action = " + action + "\nplayform = " + JsonFormat.format(platform.toString()) +
                        "\ndata = " + JsonFormat.format(data.toString()))
                dialog()

            }


            /**
             * @desc 授权失败的回调
             * *
             * @param platform 平台名称
             * *
             * @param action 行为序号，开发者用不上
             * *
             * @param t 错误原因
             */
            override fun onError(platform: SHARE_MEDIA, action: Int, t: Throwable) {

                Toast.makeText(this@MainActivity, "失败：" + t.message, Toast.LENGTH_LONG).show()
            }

            /**
             * @desc 授权取消的回调
             * *
             * @param platform 平台名称
             * *
             * @param action 行为序号，开发者用不上
             */
            override fun onCancel(platform: SHARE_MEDIA, action: Int) {

                Toast.makeText(this@MainActivity, "取消了", Toast.LENGTH_LONG).show()
            }
        }
        UMShareAPI.get(this).getPlatformInfo(this, name, authListener)
    }


    private lateinit var hintDialog: View

    private lateinit var customDialog: CustomDialog

    private fun dialog() {
        hintDialog = LayoutInflater.from(this).inflate(R.layout.dialog_common_hint, null)
        (hintDialog.findViewById(R.id.dialog_common_content) as TextView).text = resources.getString(R.string.relation_account)
        hintDialog.findViewById(R.id.dialog_common_no).setOnClickListener(View.OnClickListener {
            if (customDialog.isShowing()) {
                customDialog.dismiss()
                startActivity(Intent(this@MainActivity, LoginActivity::class.java))
            }
        })

        hintDialog.findViewById(R.id.dialog_common_yes).setOnClickListener(View.OnClickListener {
            if (customDialog.isShowing()) {
                customDialog.dismiss()
            }
            startActivity(Intent(this@MainActivity,RelationAccountActivity::class.java))
        })
        customDialog = CustomDialog(this, 0, 0, hintDialog, R.style.DialogTheme)
        customDialog.show()


    }

    companion object {
        open val REQUEST_CODE = 1
    }
}
